package com.dingup.api.template.message.util;

import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingup.api.template.message.bean.msg.*;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

import static com.dingup.api.template.message.util.DingTalkConstUtil.SERVER_SEND_MSG;

/**
 * Created by wancheng on 2018/10/17.
 */
public class DingTalkMessageUtil {

    private static Logger logger = LoggerFactory.getLogger(DingTalkMessageUtil.class);

    public static final String SENDMST_TYPE_TEXT = "text";
    public static final String SENDMST_TYPE_LINK = "link";
    public static final String SENDMST_TYPE_ACTION_CARD = "action_card";
    public static final String SENDMST_TYPE_OA = "oa";
    public static final String SENDMST_TYPE_MARKDOWN = "markdown";

    private static DingTalkClient getClient(){
        DingTalkClient client = new DefaultDingTalkClient(SERVER_SEND_MSG);
        return client;
    }
    private static OapiMessageCorpconversationAsyncsendV2Request getRequest(String agentId, String userIds){
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setUseridList(userIds);
        request.setAgentId(Long.valueOf(agentId));
        request.setToAllUser(false);
        return request;
    }

    public static Boolean SendMsg(DingTalkMsgBaseBean bean,String type){
        OapiMessageCorpconversationAsyncsendV2Response response = null;
        if (SENDMST_TYPE_TEXT.equals(type)){
            response = sendTextMsg((DingTalkTextBean)bean);
        }else if (SENDMST_TYPE_LINK.equals(type)){
            response = sendLinkMsg((DingTalkLinkBean) bean);
        }else if (SENDMST_TYPE_ACTION_CARD.equals(type)){
            response = sendActionCardMsg((DingTalkActionCardBean) bean);
        }else if (SENDMST_TYPE_OA.equals(type)){
            response = sendOaMsg((DingTalkOaBean) bean);
        }else if (SENDMST_TYPE_MARKDOWN.equals(type)){
            response = sendMarkdownMsg((DingTalkMarkdownBean) bean);
        }
        if (response.getErrcode() == 0L){
            return true;
        }else{
            logger.info("");
            return false;
        }

    }
    /**
     * 发送文本消息
     * @param bean
     * @return
     */
    public static OapiMessageCorpconversationAsyncsendV2Response sendTextMsg(DingTalkTextBean bean) {
        DingTalkClient client = getClient();
        OapiMessageCorpconversationAsyncsendV2Request request = getRequest(bean.getAgentId(),bean.getUserIds());
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype(SENDMST_TYPE_TEXT);
        msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
        msg.getText().setContent(bean.getContent());
        request.setMsg(msg);
        OapiMessageCorpconversationAsyncsendV2Response response = null;
        String accessToken = DingTalkUtil.getCorpAccessToken();
        try {
            response = client.execute(request, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        logger.info("sendTextMsg,DingTalkTextBean={},responseCode={},response={},task_id={}",
                JSON.toJSONString(bean),response.getErrcode(),response.getErrmsg(),response.getTaskId());
        return response;

    }
    public static OapiMessageCorpconversationAsyncsendV2Response sendLinkMsg(DingTalkLinkBean bean) {
        DingTalkClient client = getClient();
        OapiMessageCorpconversationAsyncsendV2Request request = getRequest(bean.getAgentId(),bean.getUserIds());
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype(SENDMST_TYPE_LINK);
        msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
        msg.getLink().setTitle(bean.getTitle());
        msg.getLink().setText(bean.getText());
        msg.getLink().setMessageUrl(bean.getMessageUrl());
        msg.getLink().setPicUrl(bean.getPicUrl());
        request.setMsg(msg);
        OapiMessageCorpconversationAsyncsendV2Response response = null;
        String accessToken = DingTalkUtil.getCorpAccessToken();
        try {
            response = client.execute(request, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        logger.info("sendLinkMsg,DingTalkLinkBean={},responseCode={},response={},task_id={}",
                JSON.toJSONString(bean),response.getErrcode(),response.getErrmsg(),response.getTaskId());
        return response;

    }

    public static OapiMessageCorpconversationAsyncsendV2Response sendActionCardMsg(DingTalkActionCardBean bean) {
        DingTalkClient client = getClient();
        OapiMessageCorpconversationAsyncsendV2Request request = getRequest(bean.getAgentId(),bean.getUserIds());
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype(SENDMST_TYPE_ACTION_CARD);
        msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
        msg.getActionCard().setTitle(bean.getTitle());
        msg.getActionCard().setMarkdown(bean.getMarkdown());
        msg.getActionCard().setSingleTitle(bean.getSingleTitle());
        msg.getActionCard().setSingleUrl(bean.getSingleUrl());
        msg.getActionCard().setBtnOrientation(bean.getBtnOrientation());
        msg.getActionCard().setBtnJsonList(bean.getBtnList());
        request.setMsg(msg);
        OapiMessageCorpconversationAsyncsendV2Response response = null;
        String accessToken = DingTalkUtil.getCorpAccessToken();
        try {
            response = client.execute(request, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        logger.info("sendActionCardMsg,DingTalkActionCardBean={},responseCode={},response={},task_id={}",
                JSON.toJSONString(bean),response.getErrcode(),response.getErrmsg(),response.getTaskId());
        return response;

    }

    public static OapiMessageCorpconversationAsyncsendV2Response sendOaMsg(DingTalkOaBean bean) {
        DingTalkClient client = getClient();
        OapiMessageCorpconversationAsyncsendV2Request request = getRequest(bean.getAgentId(),bean.getUserIds());
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
        msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
        msg.getOa().getHead().setText(bean.getText());
        msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
        msg.getOa().getBody().setContent(bean.getContent());
        msg.setMsgtype(SENDMST_TYPE_OA);
        request.setMsg(msg);
        OapiMessageCorpconversationAsyncsendV2Response response = null;
        String accessToken = DingTalkUtil.getCorpAccessToken();
        try {
            response = client.execute(request, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        logger.info("sendOaMsg,DingTalkOaBean={},responseCode={},response={},task_id={}",
                JSON.toJSONString(bean),response.getErrcode(),response.getErrmsg(),response.getTaskId());
        return response;

    }
    public static OapiMessageCorpconversationAsyncsendV2Response sendMarkdownMsg(DingTalkMarkdownBean bean) {
        DingTalkClient client = getClient();
        OapiMessageCorpconversationAsyncsendV2Request request = getRequest(bean.getAgentId(),bean.getUserIds());
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype(SENDMST_TYPE_MARKDOWN);
        msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
        msg.getMarkdown().setText(bean.getText());
        msg.getMarkdown().setTitle(bean.getTitle());
        request.setMsg(msg);
        OapiMessageCorpconversationAsyncsendV2Response response = null;
        String accessToken = DingTalkUtil.getCorpAccessToken();
        try {
            response = client.execute(request, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        logger.info("sendOaMsg,DingTalkOaBean={},responseCode={},response={},task_id={}",
                JSON.toJSONString(bean),response.getErrcode(),response.getErrmsg(),response.getTaskId());
        return response;

    }

}
